/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package core;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author ddoose
 */
public class Matrix {
    private List<List<Integer>> lines;
    
    public Matrix() {
        lines = new ArrayList<List<Integer>>();
    }
    
    public void addLine() {
        List<Integer> line = new ArrayList<Integer>();
        lines.add(line);
    }
    
    public List<Integer> getLine(int l) {
        if (l >= lines.size()) {
            return null;
        }
        else {
            return lines.get(l);
        }
    }
    
    public static int getIndex(List<Integer> line, int value) {
        for (int i = 0; i < line.size(); i++) {
            if (value == line.get(i)) {
                return i;
            }
        }
        return -1;
    }
    
//    public static int getIndex(List<Integer> line, int value) {
//        return getIndex(line, value, 0, line.size()-1);
//    }
//    
//    private static int getIndex(List<Integer> line, int value, int left, int right) {
//        if ((left > right) || (line.get(0) > value) || (line.get(line.size()-1) < value)) {
//            return -1;
//        }
//        int mid_index = (right + left) / 2;
//        int mid_value = line.get(mid_index);
//        if (mid_value == value) {
//            return mid_index;
//        }
//        if (mid_value < value) {
//            return getIndex(line, value, left, mid_index-1);
//        }
//        else {
//            return getIndex(line, value, mid_index+1, right);
//        }
//    }
    
    public void addElement(int line, int value) {
        while (lines.size() <= line) {
            addLine();
        }
        
        List<Integer> temp = lines.get(line);
        if (!temp.contains(temp)) {
            temp.add(value);
//            Collections.sort(temp, IntegerComparator.IDENT);
        }
    }
    
    public void removeElement(int line, int value) {
        if (lines.size() > line) {
            List<Integer> temp = lines.get(line);
            int index = getIndex(temp, value);
            if (index != -1) {
                temp.remove(index);
            }
        }
    }
    
    public void replace(int line, int oldElt, int newElt) {
        List<Integer> temp = getLine(line);
        for (int i = 0; i < temp.size(); i++) {
            if (temp.get(i) == oldElt) {
                temp.set(i, newElt);
            }
        }
    }
    
    public Matrix clone() {
        Matrix matrix = new Matrix();
        
        for (List<Integer> line: lines) {
            List<Integer> temp = new ArrayList<Integer>();
            for (int i: line) {
                temp.add(i);
            }
            matrix.lines.add(temp);
      }
        
      return matrix;  
    }
}
